package com.eric.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.util.SaResult;
import com.eric.domain.dto.MaterialDto;
import com.eric.domain.po.Material;
import com.eric.service.IMaterialService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * <p>
 * 材料表 前端控制器
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/material")
public class MaterialController {
    private final IMaterialService materialService;

    //    新添加材料
    @PostMapping("/saveMaterial")
    public SaResult saveMaterial(MaterialDto materialDto, MultipartFile[] materialFiles, HttpServletRequest request) {
        return materialService.saveMaterialService(materialDto,materialFiles,request);
    }

    //    分页查找所有材料
    @GetMapping("/getAllByPage")
    public SaResult getAllByPage(Integer current,Integer pageSize){
        return materialService.getAllByPageService(current,pageSize);
    }


//  不分页获取所有数据
    @GetMapping("/getAll")
    public SaResult getAll(){
        return materialService.getAllService();
    }


    //    根据Id查找一条信息
    @GetMapping("/{id}")
    public SaResult getOneById(@PathVariable("id") Integer id){
        return materialService.getOneByIdService(id);
    }

    //    根据编号查询
    @GetMapping("/getByEncode")
    public SaResult getByEncode(String encode){
        return materialService.getOneByEncodeService(encode);
    }

    //    修改一条数据
    @PutMapping("/modifyOne")
    @SaCheckPermission("sys:modify")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult modifyOne(MaterialDto materialDto,MultipartFile[] materialFile, HttpServletRequest request,String[] photoList){
        return materialService.modifyOne(materialDto,materialFile,request,photoList);
    }

    //    删除一条数据
    @DeleteMapping("/{id}")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    @SaCheckPermission("sys:delete")
    public SaResult deleteOneById(@PathVariable("id") Integer id){
        return materialService.deleteOneByIdService(id);
    }

    //    根据材料名称查询
    @GetMapping("/getByMaterialName")
    public SaResult getByMaterialName(String materialName,Integer current,Integer pageSize){
        return materialService.getByMaterialNameService(materialName,current,pageSize);
    }

    //    根据模糊查询到的材料名称统计该项材料项总数
    @GetMapping("/getAllMaterialTermsByMaterlName")
    public SaResult getAllMaterialTerms(String materialName){
        return materialService.getAllMaterialsTermsByMaterialNameService(materialName);
    }

    //    查询表中所有数据条数
    @GetMapping("/getAllMaterialTerms")
    public SaResult getAllMaterialTerms(){
        return materialService.getAllMaterialTerms();
    }

    //  todo 好好想想  根据材料子类型并根据Number数量排序查询，
    @GetMapping("/getByTypeName")
    public SaResult getByTypeName(String typeName,Integer orderBy,Integer current,Integer pageSize){
        return materialService.getByTypeName(typeName,orderBy,current,pageSize);
    }

//    配合getByTypeName方法，获取根据typeName查询到得数据获取总条数
    @GetMapping("/getCountsByTypeName")
    public SaResult getCountsByTypeName(String typeName){
        return materialService.getCountsByTypeName(typeName);
    }

    //    多功能名字查询
    @GetMapping("/getByMultifunction")
    public SaResult getByMultifunction(String name,Integer current,Integer pageSize){
        return materialService.getByMultifunctionService(name,current,pageSize);
    }
    //    多功能名字查询-数据条数
    @GetMapping("/getCountByMultifunctionName")
    public SaResult getCountByMultifunctionName(String name){
        return materialService.getCountByMultifunctionNameService(name);
    }

//    下载多个文件接口，并打包成zip文件
    @PostMapping("/downLoadFiles")
    public void downloadFiles(@RequestBody String[]files, HttpServletResponse response)throws IOException{
        materialService.downloadFilesService(files,response);
    }

//    变更材料状态
    @PutMapping("/modifyMaterialStatus")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult modifyMaterialStatus(@RequestBody Material material){
        return materialService.modifyMaterialStatusService(material);
    }

//    盘点数量存储
    @PutMapping("/modifyStockTakeNumber")
    public SaResult modifyStockTakeNumber(@RequestBody Material material){
        if(Objects.equals(material.getNumber(), material.getStockTakeNumber())){
            return SaResult.error("盘点数量与材料数量相同");
        }
        return materialService.modifyStockTakeNumberService(material);
    }
//    确认最终盘点数据
    @PutMapping("/modifyFinalStockTakeNumber")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin","sys:manager"},mode = SaMode.OR)
    public SaResult modifyFinalStockTakeNumber(){
        return materialService.modifyFinalStockTakeNumberService();
    }

}
